package com.thlws.redis.datatype;

import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author HanleyTang
 * @date 2018-12-15
 */
public class JedisHash {

    public static void main(String[] args) {

        try(Jedis jedis = new Jedis("localhost",6379);){

            Map<String,String> hashMap = new HashMap<>();
            hashMap.put("name", "芒果");
            hashMap.put("price", "30");

            jedis.hmset("mymap",hashMap);

            long mapSize = jedis.hlen("mymap");
            System.out.println("map size:"+mapSize);

            System.out.println(jedis.hmget("mymap","price"));


            Map<String,String> hashResult = jedis.hgetAll("mymap");
            for (String key: hashResult.keySet() ) {
                System.out.println(key+":"+hashResult.get(key));
            }

            //得到指定key的所有value
            List<String> vals = jedis.hvals("mymap");
            for (String val:vals
                 ) {
                System.out.println(val);
            }


            //得到指定key的所有 key
            Set<String> keys = jedis.hkeys("mymap");
            for (String k:keys
                 ) {
                System.out.println(k);
            }
        }
    }
}
